from logging import log
from typing import Dict, List
from LogManager import log_output
from UserManager import LoginInfo, UserManager
from InputChecker import check_password, check_username


class RegisterManager:
    def __init__(self, user_manager: UserManager):
        self.error_info: Dict[str, str] = {
            'type': '1'
        }

        self.user_manager: UserManager = user_manager

    def register(self, send_data: Dict[str, str]) -> Dict[str, str]:
        if type(send_data) != dict:
            log_output('Register data not dict')
            return self.error_info

        if 'username' not in send_data or 'password' not in send_data:
            log_output('Register dict format error')
            return self.error_info

        username = send_data['username']
        password = send_data['password']

        if not check_username(username) or not check_password(password):
            log_output('Register Illegal username or password')
            return self.error_info

        try:
            register_result = self.user_manager.register_user_pwd(
                username, password)
            result_dict = {
                'type': '0',
                'token': register_result.token
            }
            return result_dict
        except Exception as err:
            log_output(str(err))
            return self.error_info
